/*
 * Copyright (C) 2024 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

// We only use handful of methods, no need to pull entire libc as dependency
#[allow(non_camel_case_types)]
pub mod libc {
    use std::ffi::{c_int, c_long, c_void, c_uint};

    pub const EPOLL_CLOEXEC: u32 = 524288;
    pub const EPOLL_NONBLOCK: u32 = 2048;
    pub const EPOLLIN: u32 = 0x1;

    pub type time_t = c_long;

    pub type clockid_t = c_int;
    pub const CLOCK_MONOTONIC: clockid_t = 1;
    pub const O_NONBLOCK: c_int = 2048;
    pub const TFD_NONBLOCK: c_int = O_NONBLOCK;

    #[repr(C)]
    pub struct itimerspec {
        pub it_interval: timespec,
        pub it_value: timespec,
    }

    #[repr(C)]
    pub struct timespec {
        pub tv_sec: time_t,
        #[cfg(all(target_arch = "x86_64", target_pointer_width = "32"))]
        pub tv_nsec: i64,
        #[cfg(not(all(target_arch = "x86_64", target_pointer_width = "32")))]
        pub tv_nsec: c_long,
    }

    extern "C" {
        pub fn read(fd: c_int, buf: *mut c_void, count: usize) -> isize;
        pub fn pipe(fds: *mut c_int) -> c_int;
        pub fn timerfd_create(clock: clockid_t, flags: c_int) -> c_int;
        pub fn timerfd_settime(
            fd: c_int,
            flags: c_int,
            new_value: *const itimerspec,
            old_value: *mut itimerspec,
        ) -> c_int;
        pub fn eventfd(init: c_uint, flags: c_int) -> c_int;
        pub fn close(fd: c_int) -> c_int;
        pub fn write(fd: c_int, buf: *const c_void, count: usize) -> isize;
        pub fn pthread_self() -> usize;
    }
}
